﻿package fwk.loaderManager 
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.utils.getDefinitionByName;
   	import flash.system.ApplicationDomain;
   	import flash.system.SecurityDomain;
   	import flash.system.LoaderContext;
	import flash.system.Capabilities;
	
	dynamic public class LoaderImage extends MovieClip
	{
		private var	_classReference		:Class;
		private var	_mcLoaderImage		:MovieClip;
		
		private var	_sUrl				:String;
		private var	_sLibraryLink		:String;
		private var	_bSmoothing			:Boolean;
		private var	_nWidth				:Number;
		private var	_nHeight			:Number;
		private var	_bResize			:Boolean;
		private var	_bCenter			:Boolean;
		private var _securityDomain		:Boolean;
		
		private var	_ldr				:Loader			= new Loader();
		private var	_bmpImage			:Bitmap;
		private var	_fade				:Boolean = true;
		private var _load				:Boolean;
		
		public function LoaderImage(sUrlParam:String, sLibraryLinkParam:String = null, nWidthParam:Number = 0, nHeightParam:Number = 0, bSmoothingParam:Boolean = false, bResizeParam:Boolean = false, bCenterParam:Boolean = false, securityDomain:Boolean = false, autoLoad:Boolean = true) 
		{
			//trace("LoaderImage : init(" + sUrlParam + ", " + sLibraryLinkParam + ", " + nWidthParam + ", " + nHeightParam + ", " + bSmoothingParam + ", " + bResizeParam + ", " + bCenterParam + ", " + autoLoad + ")");
			
			_sUrl			= sUrlParam;
			_sLibraryLink	= sLibraryLinkParam;
			_nWidth			= nWidthParam;
			_nHeight		= nHeightParam;
			_bSmoothing		= bSmoothingParam;
			_bResize		= bResizeParam;
			_bCenter		= bCenterParam;
			_securityDomain	= securityDomain;
			
			
			
			
			// loader visuel
			if ( _sLibraryLink != null)
			{
				_classReference = getDefinitionByName(_sLibraryLink) as Class;
				_mcLoaderImage = new _classReference();
				
				if (_nWidth != 0)
				{
					_mcLoaderImage.x = _nWidth / 2;
				}
				if (_nHeight != 0)
				{
					_mcLoaderImage.y = _nHeight / 2;
				}
				addChild(_mcLoaderImage);
			}
			
			_ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadedFunction);
			_ldr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onLoadImageError);
			
			if(autoLoad) {
				load();
			}
		}
		public function load():void {
			if (_load) {
				return;
			}
			_load = true;
			try {
				var url:String = _sUrl;
				var urlReq:URLRequest = new URLRequest(url);
				var context:LoaderContext = new LoaderContext();
				if (_securityDomain && Capabilities.playerType != "External")
				{
					context.checkPolicyFile = true;
					context.securityDomain = SecurityDomain.currentDomain;
				}
				_ldr.load(urlReq, context);
				
			}catch(error:Error) {
				//throw new Error("Erreur chargement XML: "+ error.toString());
				dispatchEvent(new IOErrorEvent(IOErrorEvent.IO_ERROR));
			}
		}
		
		public function close():void
		{
			if ( _ldr != null) {
				
				try{
					_ldr.close();
				}catch (e:Error) {
					
				}
			}
		}
		
		private function onLoadedFunction(e:Event):void
		{
			bmpImage = _ldr.content as Bitmap;
			
			
			
			bmpImage.smoothing = _bSmoothing;
			
			if (bResize)
			{
				var mcThumbnailHeightBefore:Number = bmpImage.height;
				
				bmpImage.height = _nHeight;
				bmpImage.scaleX = bmpImage.scaleY;
			}
			
			if (bCenter)
			{
				bmpImage.x = Math.round((nWidth - bmpImage.width) / 2);
				bmpImage.y = Math.round((nHeight - bmpImage.height) / 2);
			}
			
			// display
			addChild(bmpImage);
			if( _fade)
			{
				bmpImage.alpha = 0;
				this.addEventListener(Event.ENTER_FRAME, onEnterFrameAlpha);
				function onEnterFrameAlpha(evt:Event):void
				{
					if (  bmpImage.alpha + 0.1 >= 1) {
						bmpImage.alpha = 1;
						removeEventListener(Event.ENTER_FRAME, onEnterFrameAlpha);
					}else {
						bmpImage.alpha += 0.1;
					}
				}
			}
			
			if( _mcLoaderImage != null) removeChild(_mcLoaderImage);
			
			dispatchEvent(new Event(Event.COMPLETE,true));
		}
		
		private function onLoadImageError(e:Event):void
		{
			dispatchEvent(new IOErrorEvent(IOErrorEvent.IO_ERROR));
		}
		
		public function get bmpImage():Bitmap { return _bmpImage; }
		
		public function set bmpImage(value:Bitmap):void 
		{
			_bmpImage = value;
		}
		
		public function get nWidth():Number { return _nWidth; }
		
		public function set nWidth(value:Number):void 
		{
			_nWidth = value;
		}
		
		public function get nHeight():Number { return _nHeight; }
		
		public function set nHeight(value:Number):void 
		{
			_nHeight = value;
		}
		
		public function get bSmoothing():Boolean { return _bSmoothing; }
		
		public function set bSmoothing(value:Boolean):void 
		{
			_bSmoothing = value;
		}
		
		public function get bResize():Boolean { return _bResize; }
		
		public function set bResize(value:Boolean):void 
		{
			_bResize = value;
		}
		
		public function get bCenter():Boolean { return _bCenter; }
		
		public function set bCenter(value:Boolean):void 
		{
			_bCenter = value;
		}
		
		public function get mcLoaderImage():MovieClip { return _mcLoaderImage; 
		}
		
		public function get fade() : Boolean {
			return _fade;
		}
		
		public function set fade(fade : Boolean) : void {
			_fade = fade;
		}
	}
}